package com.android.djlmapp.base;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.android.djlmapp.R;
import com.android.djlmapp.application.MyApplication;
import com.android.djlmapp.tools.global.UserInfoUtils;
import com.android.djlmapp.tools.msg.MQGlideImageLoader4;
import com.android.readcard.eventbus.Event;
import com.android.readcard.eventbus.EventBusUtil;
import com.gyf.barlibrary.ImmersionBar;
import com.meiqia.meiqiasdk.imageloader.MQImage;
import com.meiqia.meiqiasdk.util.MQIntentBuilder;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.yokeyword.fragmentation.ISupportFragment;
import me.yokeyword.fragmentation.SupportFragment;

/**
 * Created by Sabirjan on 2017/11/13.
 * DX189.
 * 飞宏电子
 */

public abstract class BaseFragment extends SupportFragment {
    /**
     * 视图是否已经初初始化
     */
    protected boolean isInit = false;
    protected boolean isLoad = false;
    public View fragmentView = null;
    protected ImmersionBar mImmersionBar;
    public String tag;

    public Unbinder mUnbinder;


    public boolean isNeedAnimation = true;

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        if(isNeedAnimation){
            _mActivity.overridePendingTransition(R.anim.push_right_in, R.anim.push_left_out);
        }
    }
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mUnbinder = ButterKnife.bind(this, view);
        isInit = true;
        if (isImmersionBarEnabled())
            initImmersionBar();
        ImageView ivTopKf = view.findViewById(R.id.iv_top_kf);
        if (ivTopKf != null && ivTopKf.getVisibility() == View.VISIBLE) {
            ivTopKf.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    goToMeiQia();
                }
            });
        }
    }

    /**
     * 跳转美洽
     */
    public void goToMeiQia() {
        // 美恰客服需要的字段

        HashMap<String, String> clientInfo = new HashMap<>();
        clientInfo.put("name", UserInfoUtils.getUserInfoUtilsInstance().getUserName());
        clientInfo.put("用户类型", "店铺");
        clientInfo.put("tel", UserInfoUtils.getUserInfoUtilsInstance().getMobile());
        clientInfo.put("address", UserInfoUtils.getUserInfoUtilsInstance().getFaddress());
        clientInfo.put("上级代理商", UserInfoUtils.getUserInfoUtilsInstance().getAgent());

        MQImage.setImageLoader(new MQGlideImageLoader4()); // 如果使用的是 glide 4.x ,就得在启动美洽前加上这句
        Intent intent = new MQIntentBuilder(_mActivity)
                .setClientInfo(clientInfo)
                .setCustomizedId(MyApplication.getInstance().getRegistrationId())
                .build();
        startActivity(intent);
    }

    @Override
    public void startForResult(ISupportFragment toFragment, int requestCode) {
        super.startForResult(toFragment, requestCode);
        if(isNeedAnimation){
            _mActivity.overridePendingTransition(R.anim.push_right_in, R.anim.push_left_out);
        }
    }



    protected boolean isRegisterEventBus() {
        return false;
    }

    @Override
    public void onStart() {
        super.onStart();
        if (isRegisterEventBus()) {
            EventBusUtil.register(this); // 注册EventBus
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Event event) {
        if (event != null) {
            receiveEvent(event);
        }
    }

    /**
     * 接收到分发到事件
     *
     * @param event 事件
     */
    protected void receiveEvent(Event event) {
        // 重写此方法获取MessageEvent
    }

    @Override
    public void onStop() {
        super.onStop();
        if (isRegisterEventBus()) {
            EventBusUtil.unregister(this); // 注销EventBus
        }
    }

    public String getToken() {
        return UserInfoUtils.getUserInfoUtilsInstance().getToken();
    }

    public String getVersion() {
        return UserInfoUtils.getUserInfoUtilsInstance().getVersion();
    }

    /**
     * 是否在Fragment使用沉浸式
     *
     * @return the boolean
     */
    protected boolean isImmersionBarEnabled() {
        return false;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden && mImmersionBar != null)
            mImmersionBar.init();
    }

    /**
     * 初始化沉浸式
     */
    protected void initImmersionBar() {
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.keyboardEnable(true).navigationBarWithKitkatEnable(false).init();
    }

    /**
     * 绑定View
     */
    protected abstract void initView();

    /**
     * 绑定数据
     */
    protected abstract void initData();

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mImmersionBar != null)
            mImmersionBar.destroy();
        if (mUnbinder != null) {
            mUnbinder.unbind();
        }
    }

    @Override
    public void onDestroyView() {
        Log.e("项目异常", "------onDestroyView------" + "");
        isInit = false;
        isLoad = false;
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        super.onDestroyView();
    }


    /**
     * 界面是否销毁
     *
     * @return
     */
    public boolean fragmentIsOnDestory() {
        if (getActivity() == null) {
            return true;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            if (getActivity().isDestroyed()) {
                return true;
            }
        } else {
            if (getActivity().getSupportFragmentManager() == null || getActivity().getSupportFragmentManager().isDestroyed()) {
                return true;
            }
        }
        return false;
    }

}
